package com.lazy.app.ui.act;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.lazy.app.BuildConfig;
import com.lazy.app.R;
import com.zndroid.base.annotation.ISplashTimerType;
import com.zndroid.base.simples.SimpleSplashActivity;
import com.zndroid.utils.XUtils;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 欢迎页
 *
 * @author lzy
 */
public class WelcomeActivity extends SimpleSplashActivity {
    private ActivityResultLauncher<Intent> debugLauncher;

    @Override
    protected Intent onSupplyMainIntent() {
        Intent mainIntent = new Intent(this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return mainIntent;
    }

    @Override
    protected long onSupplyDuration() {
        return 3_000;
    }

    @Override
    protected int onSupplyTimerType() {
        return ISplashTimerType.TYPE_CAPSULE;
    }

    @Override
    protected boolean onSupplySkipSplash() {
        //TODO by lazy
        return false;
    }

    @Override
    protected View onSupplyView() {
        return getLayoutInflater().inflate(R.layout.layout_welcome, currentViewBinding().getRoot(), false);
    }

    @Override
    protected boolean isCanBackOnRunning() {
        return true;
    }

    @Override
    protected void doOnCreate(@Nullable Bundle savedInstanceState) {
        super.doOnCreate(savedInstanceState);
        debugLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK) {
                anim();
                toRestartSplash();
            }
        });
        anim();
        updateLogo();
        showDebugInfo();
    }

    @Override
    protected void doOnInitViewAfterAllReady(View rootView) {
        View view = currentViewBinding().getRoot().findViewById(R.id.iv_logo);
        view.setOnLongClickListener(v -> {
            showDebugPage();
            return true;
        });
    }

    private void showDebugInfo() {
        AtomicReference<StringBuilder> sb = new AtomicReference<>(new StringBuilder());
        //         v1.0 debug [online]
        //      Domain: Lazy_ Channel: DEV_
        //©2021 zndroid.com. All rights reserved

        //head
        sb.get().append("v");
        //APP version name
        sb.get().append(XUtils.create().appUtil().getVersionName(this));
        //build type
        sb.get().append(" ");
        sb.get().append(BuildConfig.BUILD_TYPE);
        //online or not
        sb.get().append(" ");
        sb.get().append(BuildConfig.OFFLINE ? "[offline]" : "[online]");

        //APP domain
        sb.get().append("\n");
        sb.get().append("Domain: ");
        sb.get().append(BuildConfig.FLAVOR_DOMAIN);
        sb.get().append(" ");
        //APP channel
        sb.get().append(" ");
        sb.get().append("Channel: ");
        sb.get().append(BuildConfig.FLAVOR_CHANNEL);
        sb.get().append(" ");

        //CopyRight
        sb.get().append("\n");
        sb.get().append("©2021 zndroid.com. All rights reserved");

        AppCompatTextView textView = currentViewBinding().getRoot().findViewById(R.id.tv_app_info);
        textView.setText(sb.toString());

    }

    private void updateLogo() {
        AppCompatImageView imageView = currentViewBinding().getRoot().findViewById(R.id.iv_logo);
        imageView.setImageResource(getApplicationInfo().icon);
    }

    private void anim() {
        TextView appName = currentViewBinding().getRoot().findViewById(R.id.tv_app_slogan);
        appName.setVisibility(View.VISIBLE);

        if (null != appName.getAnimation()) {
            appName.clearAnimation();
        }

        ObjectAnimator anim = ObjectAnimator.ofFloat(appName, "alpha", 0, 1);
        anim.setDuration(onSupplyDuration() - 1_000);
        anim.setRepeatMode(ObjectAnimator.REVERSE);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                appName.setVisibility(View.INVISIBLE);
            }
        });
        anim.start();
    }

    private void showDebugPage() {
        setInterceptJump(true);
        currentViewBinding().getRoot().findViewById(R.id.tv_app_slogan).clearAnimation();

        Intent intent = new Intent(this, DebugActivity.class);
        debugLauncher.launch(intent);
    }
}
